<?php
/**
 *  Created by Mient-jan Stelling [mient-jan@mediamonks.com].
 *  Copyright (c) 2008 MediaMonks. All rights reserved.
 */

/**
 * Spectus_Table_Hierarch_Row_Abstract
 *
 * @todo major recode of class 
 * @author Mient-jan Stelling
 **/
class Spectus_Hierarch_Table_Row_Abstract extends Spectus_Default_Table_Row_Abstract
{
	public function delete()
	{
		$strTableName = $this->_table->info('name');
		$objDb = $this->getTable()->getAdapter();
		
		$intLeft = $this->lft;
		$intRight = $this->rgt;
		
		$intWidth = ($this->rgt - $this->lft) + 1;
		
		
		$objDb->query('
			DELETE FROM ' . $strTableName . '
			WHERE lft > "' . (int) $intLeft . '" AND lft < "' . (int) $intRight . '"
		');
		
		$objDb->query('UPDATE ' . $strTableName . ' SET rgt = rgt - ' . (int) $intWidth . ' WHERE rgt > ' . (int) $intRight);
		$objDb->query('UPDATE ' . $strTableName . ' SET lft = lft - ' . (int) $intWidth . ' WHERE lft > ' . (int) $intRight);
		
		return parent::delete();
	}
	
	public function save()
	{
		if( (int) $this->_data['lft'] == 0 && (int) $this->_data['rgt'] == 0 )
		{
		
			$strTableName = $this->_table->info('name');
			$objDb = $this->getTable()->getAdapter();
			
			$objDb->query('UPDATE ' . $strTableName . ' SET lft = lft + 2 WHERE lft > ?', 0 );
			$objDb->query('UPDATE ' . $strTableName . ' SET rgt = rgt + 2 WHERE rgt > ?', 1 );
			
			$this->lft = 1;
			$this->rgt = 2;
		}
		
		$strUrlColumn = $this->_table->info('urlColumn');
		$strPermaUrlColumn = $this->_table->info('permaUrlColumn');

		$this->$strUrlColumn = trim( $this->$strUrlColumn );
		$this->$strPermaUrlColumn = MediaMonks_String::urlSafeString( $this->$strUrlColumn );
		
		parent::save();
	}

	public function insertInside( Spectus_Hierarch_Table_Row_Abstract $argObjRow )
	{
		$strTableName = $this->_table->info('name');
		$objDb = $this->getTable()->getAdapter();
		
		$objDb->query('UPDATE ' . $strTableName . ' SET lft = lft + 2 WHERE lft > ?', $argObjRow->lft );
		$objDb->query('UPDATE ' . $strTableName . ' SET rgt = rgt + 2 WHERE rgt > ?', $argObjRow->lft );
		
		$this->lft = $argObjRow->lft + 1;
		$this->rgt = $argObjRow->lft + 2;
		$this->save();
	}
	
	public function insertAbove( Spectus_Hierarch_Table_Row_Abstract $argObjRow )
	{
		$strTableName = $this->_table->info('name');
		$objDb = $this->getTable()->getAdapter();
		
		$intNodeLft = $argObjRow->lft;
		
		$objDb->query('UPDATE ' . $strTableName . ' SET lft = lft + 2 WHERE lft > ?', ( $intNodeLft - 1 ) );
		$objDb->query('UPDATE ' . $strTableName . ' SET rgt = rgt + 2 WHERE rgt > ?', ( $intNodeLft - 1 ) );

		$this->lft = $intNodeLft;
		$this->rgt = $intNodeLft + 1;
		
		if( $this->rgt < 2 ){ $this->rgt = 2; }
		if( $this->lft < 1 ){ $this->lft = 1; }
		
		$this->save();
	}
	
	public function insertBelow( Spectus_Hierarch_Table_Row_Abstract $argObjRow )
	{
		$strTableName = $this->_table->info('name');
		$objDb = $this->getTable()->getAdapter();
		
		$objDb->query('UPDATE ' . $strTableName . ' SET lft = lft + 2 WHERE lft > ?', $argObjRow->rgt );
		$objDb->query('UPDATE ' . $strTableName . ' SET rgt = rgt + 2 WHERE rgt > ?', $argObjRow->rgt );

		$this->rgt = $argObjRow->rgt + 2;
		$this->lft = $argObjRow->rgt + 1;
		$this->save();
		
		// $objDb->query('UPDATE ' . $strTableName . ' SET lft = lft + 2 WHERE lft > ?', $argObjRow->rgt );
		// $objDb->query('UPDATE ' . $strTableName . ' SET rgt = rgt + 2 WHERE rgt > ?', $argObjRow->rgt );
	}
	
	public function __get( $argStrName )
	{
		switch( $argStrName )
		{
			case 'Children':
			{
				// $this->_table->get
				break;	
			}
			
			default:
			{
				return parent::__get( $argStrName );
				break;
			}
		}
	}
}